import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/components/Layout.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home.vue')
      },
      // {
      //   path: '/dashboard',
      //   name: 'Dashboard',
      //   component: () => import('@/views/Dashboard.vue'),
      //   meta: { requiresAuth: true }
      // },
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('@/views/Profile.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/exam-booking',
        name: 'ExamBooking',
        component: () => import('@/views/ExamBooking.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/learning-progress',
        name: 'LearningProgress',
        component: () => import('@/views/LearningProgress.vue'),
        meta: { requiresAuth: true }
      },
      // {
      //   path: '/messages',
      //   name: 'Messages',
      //   component: () => import('@/views/Messages.vue'),
      //   meta: { requiresAuth: true }
      // },
      {
        path: '/exams',
        name: 'Exams',
        component: () => import('@/views/Exams.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/exam-room',
        name: 'ExamRoom',
        component: () => import('@/views/ExamRoom.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/exam-result',
        name: 'ExamResult',
        component: () => import('@/views/ExamResult.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/exam-detail/:id',
        name: 'ExamDetail',
        component: () => import('@/views/ExamDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/exam-history',
        name: 'ExamHistory',
        component: () => import('@/views/ExamHistory.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/wrong-questions',
        name: 'WrongQuestions',
        component: () => import('@/views/WrongQuestions.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/question-review',
        name: 'QuestionReview',
        component: () => import('@/views/QuestionReview.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/exam/tokutei',
        name: 'TokuteiExam',
        component: () => import('@/views/TokuteiExam.vue')
      },
    {
      path: '/exam/examAll',
        name: 'ExamAll',
      component: () => import('@/views/ExamAll.vue')
    },
      {
        path: '/exam/jft',
        name: 'JftExam',
        component: () => import('@/views/JftExam.vue')
      },
      {
        path: '/exam/jlpt',
        name: 'JlptExam',
        component: () => import('@/views/JlptExam.vue')
      },
      {
        path: '/exam/interview',
        name: 'InterviewTraining',
        component: () => import('@/views/InterviewTraining.vue')
      },
      {
        path: '/materials',
        name: 'Materials',
        component: () => import('@/views/Materials.vue')
      },
      {
        path: '/videos',
        name: 'Videos',
        component: () => import('@/views/Videos.vue')
      },
      {
        path: '/jobs',
        name: 'Jobs',
        component: () => import('@/views/Jobs.vue')
      },
      {
        path: '/about',
        name: 'About',
        component: () => import('@/views/About.vue')
      },
      {
        path: '/contact',
        name: 'Contact',
        component: () => import('@/views/Contact.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router 